package com.jqkb.pages;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;

import com.fasterxml.jackson.databind.JsonNode;
import com.jqkb.R;
import com.jqkb.adapters.PendingForwardAdapter;
import com.jqkb.service.ActionService;
import com.jqkb.service.bean.Action;
import com.jqkb.service.util.Converter;
import com.jqkb.service.util.ListCallBack;
import com.jqkb.service.util.SimpleApi;
import com.jqkb.utils.Constant;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import java.util.ArrayList;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Call;

public class PendingForwardActivity extends AppCompatActivity implements OnRefreshListener, OnRefreshLoadmoreListener {

    private RecyclerView recyclerView;
    private List<String> list;
    private SmartRefreshLayout refreshLayout;
    private int page = 1;
    private PendingForwardAdapter adapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_info_list);
        list = new ArrayList<>();
        recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new PendingForwardAdapter(this);
        recyclerView.setAdapter(adapter);
        refreshLayout = (SmartRefreshLayout) findViewById(R.id.refreshLayout);
        refreshLayout.autoRefresh();


        refreshLayout.setOnRefreshListener(this);
        refreshLayout.setOnRefreshLoadmoreListener(this);

    }

    public void toBack(View view) {
        finish();
    }

    @Override
    public void onRefresh(final RefreshLayout refreshlayout) {


        ActionService service = SimpleApi.create(ActionService.class);
        Call<ResponseBody> responseBodyCall = service.actionForwardListPending(Constant.PAGE_SIZE, 1);

        page = 1;

        Converter.convert(Action.class, responseBodyCall, new ListCallBack<Action>() {


            @Override
            public void onSuccess(Object object, JsonNode data) {


                List<Action> actions = (List<Action>) object;
                adapter.setData(actions);
                refreshlayout.finishRefresh();

//                Toast.makeText(PendingForwardActivity.this, ""+actions.size(), Toast.LENGTH_SHORT).show();


            }

            @Override
            public void onFail(boolean success, int code, String message, JsonNode data) {

                Toast.makeText(PendingForwardActivity.this, message, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNetworkFail(Call<ResponseBody> jsonNode) {
                Toast.makeText(PendingForwardActivity.this, "网路连接失败", Toast.LENGTH_SHORT).show();
            }
            @Override
            public void onError(Call<ResponseBody> response) {
            }
        });


    }


    @Override
    public void onLoadmore(final RefreshLayout refreshlayout) {
        ActionService service = SimpleApi.create(ActionService.class);
        Call<ResponseBody> responseBodyCall = service.actionForwardListPending(Constant.PAGE_SIZE, page + 1);

        Converter.convert(Action.class, responseBodyCall, new ListCallBack<Action>() {


            @Override
            public void onSuccess(Object object, JsonNode data) {

                page += 1;
                List<Action> actions = (List<Action>) object;
                adapter.addData(actions);
                refreshlayout.finishLoadmore();

//                Toast.makeText(PendingForwardActivity.this, ""+actions.size(), Toast.LENGTH_SHORT).show();


            }

            @Override
            public void onFail(boolean success, int code, String message, JsonNode data) {
            }

            @Override
            public void onNetworkFail(Call<ResponseBody> jsonNode) {
            }

            @Override
            public void onError(Call<ResponseBody> response) {
            }
        });


    }
}
